home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Communications / pcomm / Source / d_print.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-12  |  3.9 KB  |  189 lines

  1. /*
  2.  * The print option of the dialing directory.  A carriage return will
  3.  * send the dialing directory to the print spool program, otherwise the
  4.  * selected file will be used.
  5.  */
  6.  
  7. #define MAX_STRING    80
  8.  
  9. #include <stdio.h>
  10. #include <curses.h>
  11. #include "config.h"
  12. #include "dial_dir.h"
  13. #include "misc.h"
  14.  
  15. void
  16. print_dir()
  17. {
  18.     FILE *fp, *popen(), *my_fopen();
  19.     WINDOW *p_win, *newwin();
  20.     char *ans, *file, buf[100], *expand();
  21.     static char *e_get_str();
  22.     int is_printer, i;
  23.     void error_win();
  24.     unsigned int sleep();
  25.  
  26.     p_win = newwin(5, 54, 0, 26);
  27.  
  28.     mvwaddstr(p_win, 2, 3, "Print to: (printer)");
  29.     box(p_win, VERT, HORZ);
  30.     wmove(p_win, 2, 13);
  31.     wrefresh(p_win);
  32.  
  33.     /*
  34.      * This is a special version of get_str() that looks at the
  35.      * first character to see if it should erase the default answer
  36.      * already on the screen.
  37.      */
  38.     if ((ans = e_get_str(p_win, 80)) == NULL) {
  39.                     /* erase because it overlaps dm_win */
  40.         werase(p_win);
  41.         wrefresh(p_win);
  42.         delwin(p_win);
  43.         return;
  44.     }
  45.     file = expand(ans);
  46.     is_printer = 0;
  47.                     /* the default (printer) */
  48.     if (*file == '\0') {
  49.         if (!(fp = popen(LPRINT, "w"))) {
  50.             sprintf(buf, "\"%s\"", LPRINT);
  51.             error_win(0, "Can't open printer program", buf);
  52.             werase(p_win);
  53.             wrefresh(p_win);
  54.             delwin(p_win);
  55.             return;
  56.         }
  57.         is_printer++;
  58.     }
  59.                     /* the requested file */
  60.     else {
  61.         /*
  62.          * Check to see if the file already exists (and if you
  63.          * have write permission too).  Currently only allows
  64.          * you to bail out or overwrite the file (no append).
  65.          */
  66.         switch(can_write(file)) {
  67.             case DENIED:
  68.                 sprintf(buf, "\"%s\"", file);
  69.                 error_win(0, "No write permission on file", buf);
  70.                 werase(p_win);
  71.                 wrefresh(p_win);
  72.                 delwin(p_win);
  73.                 return;
  74.             case OK_BUT_EXISTS:
  75.                 werase(p_win);
  76.                 mvwprintw(p_win, 2, 3, "File \"%s\" already exists!", file);
  77.                 beep();
  78.                 box(p_win, VERT, HORZ);
  79.                 if (!yes_prompt(p_win, 3, 3, A_BOLD, "Overwrite")) {
  80.                     werase(p_win);
  81.                     wrefresh(p_win);
  82.                     delwin(p_win);
  83.                     return;
  84.                 }
  85.                 /* fall thru */
  86.             case WRITE_OK:
  87.                 if (!(fp = my_fopen(file, "w"))) {
  88.                     sprintf(buf, "\"%s\"", file);
  89.                     error_win(0, "Can't open file", buf);
  90.                     werase(p_win);
  91.                     wrefresh(p_win);
  92.                     delwin(p_win);
  93.                     return;
  94.                 }
  95.                 break;
  96.         }
  97.     }
  98.  
  99.     werase(p_win);
  100.     mvwaddstr(p_win, 2, 13, "Printing Pcomm directory");
  101.     box(p_win, VERT, HORZ);
  102.     wrefresh(p_win);
  103.  
  104.     /*
  105.      * Only prints up to the end of the physical file, not the entire
  106.      * structure.  I gave some thought about not printing empty entries,
  107.      * but...
  108.      */
  109.     for (i=1; i<=dir->d_entries; i++)
  110.         fprintf(fp, "%4d- %-20.20s %18.18s  %5d-%c-%d-%d  %c  %-14.14s\n",
  111.          i, dir->name[i], dir->number[i], dir->baud[i], dir->parity[i],
  112.          dir->dbits[i], dir->sbits[i], dir->duplex[i], dir->script[i]);
  113.  
  114.     if (is_printer)
  115.         pclose(fp);
  116.     else {
  117.                     /* a dramatic delay... */
  118.         sleep(1);
  119.         fclose(fp);
  120.     }
  121.  
  122.     werase(p_win);
  123.     wrefresh(p_win);
  124.     delwin(p_win);
  125.     return;
  126. }
  127.  
  128. /*
  129.  * Get a string from a window but erase the line first.
  130.  */
  131.  
  132. static char *
  133. e_get_str(win, num)
  134. WINDOW *win;
  135. int num;
  136. {
  137.     int count, x, y, done_it;
  138.     char ans;
  139.     static char buf[MAX_STRING];
  140.  
  141.     done_it = 0;
  142.     count = 0;
  143.     while ((ans = wgetch(win)) != '\r') {
  144.                     /* do our own backspace */
  145.         if (ans == BS || ans == DEL) {
  146.             if (!count) {
  147.                 beep();
  148.                 continue;
  149.             }
  150.             count--;
  151.             buf[count] = '\0';
  152.             getyx(win, y, x);
  153.             x--;
  154.             wmove(win, y, x);
  155.             waddch(win, (chtype) ' ');
  156.             wmove(win, y, x);
  157.             wrefresh(win);
  158.             continue;
  159.         }
  160.                     /* exceeded the max? */
  161.         if (count >= num || count >= MAX_STRING) {
  162.             beep();
  163.             continue;
  164.         }
  165.                     /* illegal character? */
  166.         if (ans == '\n') {
  167.             beep();
  168.             continue;
  169.         }
  170.                     /* an <ESC> anywhere in the string */
  171.         if (ans == ESC)
  172.             return(NULL);
  173.                     /* erase the default answer */
  174.         if (!done_it) {
  175.             waddstr(win, "         ");
  176.             wmove(win, 2, 13);
  177.             wrefresh(win);
  178.             done_it++;
  179.         }
  180.  
  181.         buf[count] = ans;
  182.         waddch(win, (chtype) ans);
  183.         wrefresh(win);
  184.         count++;
  185.     }
  186.     buf[count] = '\0';
  187.     return(buf);
  188. }
  189.